<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>企业准入管理</title>
    <link rel="stylesheet" href="../../component/pear/css/pear.css"/>
</head>
<body class="pear-container">
<style>
    .edit-btn {
        border: 0;
        background: 0;
        color: #0000FF;
        font-size: 14px;
    }

    .edit-btn:hover {
        border: 0;
        background: 0;
        font-size: 14px;
    }

    .layui-form-label {
        width: 120px;
    }
</style>


<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">设备编号</label>
                    <div class="layui-input-inline">
                        <input type="text" name="b1.camera_num\@like" class="layui-input" autocomplete="off">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">白名单</label>
                    <div class="layui-input-inline">
                        <select name="block" xm-selects="block" xm-select-skin="default">
                            <option value="">请选择</option>
                            <option value="1">是</option>
                            <option value="0">否</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="bean-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>

<div class="layui-card">
    <div class="layui-card-body">
        <button class="pear-btn pear-btn-info pear-btn-md" id="uploadAction" style="display: none">
            <i class="layui-icon layui-icon-upload"></i>
            上传
        </button>
        <table id="bean-table" lay-filter="bean-table"></table>
    </div>
</div>

<script type="text/html" id="bean-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
        <i class="layui-icon layui-icon-delete"></i>
        删除
    </button>
</script>
<script type="text/html" id="bean-bar">
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit" title="编辑"><i
            class="layui-icon layui-icon-edit"></i></button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove" title="删除"><i
            class="layui-icon layui-icon-delete"></i></button>
</script>

<script type="text/html" id="block-enable">
    <input type="checkbox" name="block" value="{{d.id}}" lay-skin="switch" lay-text="是|否" lay-filter="block-enable"  {{d.block==1?'checked':''}} />
</script>

</script>
<script src="../../component/layui/layui.js"></script>
<script src="../../component/pear/pear.js"></script>
<script>
    layui.use(['table', 'form', 'jquery', 'common','upload','toast'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;
        let upload = layui.upload;
        let toast = layui.toast;
        let VIEW_PATH = "${ctxPath}/allow/view/";
        let DATA_PATH = "${ctxPath}/allow/data/";

        let cols = [[
            {type: 'checkbox'},
            {title: '设备编号', field: 'cameraNum', align: 'center', width: 220},
            {title: '设备名称', field: 'cameraName', align: 'center', width: 180},
            {title: '白名单', field: 'block', align: 'center', templet: '#block-enable'},
            {title: '操作', toolbar: '#bean-bar', align: 'center', width: 120}
        ]]

        table.render({
            elem: '#bean-table',
            url: DATA_PATH + 'list',
            page: true,
            cols: cols,
            skin: 'line',
            defaultToolbar: "",
            height: 'full-180',
            toolbar: '#bean-toolbar'
        });

        table.on('tool(bean-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            }else if (obj.event === 'edit') {
                window.saveOrUpdate(obj);
            }
        });

        table.on('toolbar(bean-table)', function (obj) {
            if (obj.event === 'add') {
                saveOrUpdate(null);
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            }
        });

        form.on('submit(bean-query)', function (data) {
            table.reload('bean-table', {
                where: data.field,page: {curr: 1}
            })
            return false;
        });

        form.on('switch(block-enable)', function(obj) {
            let loading = layer.load();
            $.ajax({
                url: DATA_PATH+"block?id="+this.value+"&block="+(obj.elem.checked?1:0),
                dataType:'json',
                type:'post',
                success:function(result){
                    layer.close(loading);
                    if(result.code == 0) {
                        toast.success({title: "系统消息", message: result.message,position: 'topRight'});
                    }else{
                        toast.error({title: "系统消息",message: result.message,position: 'topRight'});
                    }
                }
            });
        });

        window.saveOrUpdate = function (obj) {
            layer.open({
                type: 2,
                // title: '新增',
                title: obj ? '修改轮巡白名单' : '新增轮巡白名单',
                shade: 0.1,
                area: [common.isModile() ? '100%' : '500px', common.isModile() ? '100%' : '500px'],
                content: VIEW_PATH + 'saveOrUpdate' + (obj ? "?id=" + obj.data.id : "")
            });
        }

        var uploadInst = upload.render({
            elem: '#uploadAction' //绑定元素
            , url: DATA_PATH + 'importData' //上传接口
            , accept: 'file'
            , exts: 'xls|xlsx|xlsm'
            , before: function (obj) {
                layer.load(); //上传loading
            }
            , done: function (res) {
                console.log(res);
                //上传完毕回调
                if (res.code == 0) {
                    layer.closeAll('loading'); //关闭loading
                    if (res.data.code==200){
                        layer.msg("操作成功，共计导入条数：" + res.data.size+"，覆盖已存在记录："+res.data.sameCount+"条", {
                            icon: 1,
                            time: 2000
                        }, function () {
                            table.reload('bean-table');
                        });
                    }else{
                        layer.msg("操作失败，提示：" + res.data.msg, {
                            icon: 2,
                            time: 4000
                        }, function () {

                        });
                    }
                }
            }
            , error: function () {
                layer.closeAll('loading'); //关闭loading
                //请求异常回调
                layer.msg("操作失败", {
                    icon: 2,
                    time: 1000
                });
            }
        });

        window.remove = function (obj) {
            layer.confirm('确定要删除该记录', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: DATA_PATH + "remove?ids=" + obj.data['id'],
                    dataType: 'json',
                    type: 'post',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code == 0) {
                            layer.msg("操作成功", {
                                icon: 1,
                                time: 1000
                            }, function () {
                                obj.del();
                            });
                        } else {
                            layer.msg("操作失败", {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        window.batchRemove = function (obj) {
            var checkIds = common.checkField(obj, 'id');
            if (checkIds === "") {
                layer.msg("未选中数据", {
                    icon: 0,
                    time: 1000
                });
                return false;
            }
            layer.confirm('确定要删除这些记录', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: DATA_PATH + "remove?ids=" + checkIds,
                    dataType: 'json',
                    type: 'post',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code == 0) {
                            layer.msg("操作成功", {
                                icon: 1,
                                time: 1000
                            }, function () {
                                table.reload('bean-table');
                            });
                        } else {
                            layer.msg("操作失败", {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        window.refresh = function (param) {
            table.reload('bean-table');
        }
    })
</script>
</body>
</html>
